﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlowersStore
{
	public partial class OrderDetails : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (Request.UrlReferrer != null)
					ViewState["PreviousPageUrl"] = Request.UrlReferrer.ToString();
				long orderId = -1;
				if (Request.QueryString["order"] == null || !long.TryParse(Request.QueryString["order"], out orderId))
					Response.Redirect("AdminOrders.aspx");

				FlowerStoreDataContext db = new FlowerStoreDataContext();
				var order = (from o in db.Orders
							 where o.Order_id == orderId
							 select o).SingleOrDefault();
				if (order == null)
					Response.Redirect("AdminOrders.aspx");

				pAddInfo.InnerText = order.Order_additionalInfo;
				lblAddress.Text = order.Order_address;
				lblDelivery.Text = string.Format("{0:d} {1:t} - {2:t}", order.Order_deliveryDT, order.Order_deliveryStartTime, order.Order_deliveryEndTime);
				lblOrderDate.Text = string.Format("{0:d} {0:t}", order.Order_DT);

				lblReceiverInfo.Text = string.Format("Имя: {0}, {1}",order.Person.Person_name,order.Person.Person_phone);
				pRecieverAdInfo.InnerText = order.Person.Person_additionalInfo;

				lblSenderInfo.Text = string.Format("Имя: {0}, {1}", order.Person1.Person_name, order.Person1.Person_phone);
				pSenderAddInfo.InnerText = order.Person1.Person_additionalInfo;


				lblOrderNumber.Text = order.Order_id.ToString();
				lblOrderStatus.Text = order.OrderStatus.OrderStatus_name;
				switch (order.OrderStatus_id)
				{
					case 0:
					case 3:
					case 4:
						ddlActions.Visible = false;
						btnDoAction.Visible = false;
						break;
					case 1:
						btnDoAction.Visible = true;
						ddlActions.Items.Add(new ListItem("Завершить","0"));
						ddlActions.Items.Add(new ListItem("Отменить","4"));
						break;
					case 2:
						btnDoAction.Visible = true;
						ddlActions.Items.Add(new ListItem("Принять в обработку","1"));
						ddlActions.Items.Add(new ListItem("Отклонить заказ","3"));
						break;
				}
				lblTotalPrice.Text = order.Order_totalPrice.ToString();
			}
			//switch (order.OrderStatus_id)
			//{
			//    case 0:
			//        imgOrderStatus.ImageUrl = "~/Styles/Images/Done.png";
			//        break;
			//    case 1:
			//        imgOrderStatus.ImageUrl = "~/Styles/Images/InProgress.png";
			//        break;
			//    case 2:
			//        imgOrderStatus.ImageUrl = "~/Styles/Images/new.png";
			//        break;
			//    case 3:
			//        imgOrderStatus.ImageUrl = "~/Styles/Images/Denied.png";
			//        break;
			//    case 4:
			//        imgOrderStatus.ImageUrl = "~/Styles/Images/cancel.png";
			//        break;
			//}
		    
		}

		protected void btnBack_Click(object sender, EventArgs e)
		{
			if (ViewState["PreviousPageUrl"] != null)
				Response.Redirect(ViewState["PreviousPageUrl"].ToString());
		}

		protected void btnDoAction_Click(object sender, EventArgs e)
		{
			int status = int.Parse(ddlActions.SelectedValue);
			long orderId = long.Parse(Request.QueryString["order"]);
			using (FlowerStoreDataContext db = new FlowerStoreDataContext())
			{
				var order = (from o in db.Orders
							 where o.Order_id == orderId
							 select o).SingleOrDefault();
				OrderStatus st = (from os in db.OrderStatus
								  where os.OrderStatus_id == status
								  select os).SingleOrDefault();
				order.OrderStatus = st;				
				db.SubmitChanges();
			}
			if (ViewState["PreviousPageUrl"] != null)
				Response.Redirect(ViewState["PreviousPageUrl"].ToString());
		}
	}
}